--[[
	GMod Last Man Standing, a gamemode for Garry's Mod
    Copyright (C) 2012 Wickex

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

--Attempt 2 - 4/5/2013 - REMAKE GLMS.

AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("player_ext_sh.lua")

include("shared.lua")
include("utilities.lua")
include("weapons.lua")
include("player.lua")
include("player_ext.lua")
include("player_ext_sh.lua")


CreateConVar("glms_debug", "1", FCVAR_NOTIFY)
CreateConVar("glms_gracetime", "10", FCVAR_NOTIFY)
CreateConVar("glms_initialgracetime", "15", FCVAR_NOTIFY)
CreateConVar("glms_roundtime", "1", FCVAR_NOTIFY)
CreateConVar("glms_additionaltime", "0.5", FCVAR_NOTIFY)
CreateConVar("glms_endtime", "10", FCVAR_NOTIFY)
CreateConVar("glms_minimum_players", "2", FCVAR_NOTIFY)
--CreateConVar("glms_minimum_players", "2", FCVAR_NOTIFY)
--CreateConVar("glms_debug", "0", FCVAR_NOTIFY)
--CreateConVar("glms_gracetime", "30", FCVAR_NOTIFY)
--CreateConVar("glms_initialgracetime", "60", FCVAR_NOTIFY)
--CreateConVar("glms_roundtime", "15", FCVAR_NOTIFY)
--CreateConVar("glms_additionaltime", "2", FCVAR_NOTIFY)
--CreateConVar("glms_endtime", "30", FCVAR_NOTIFY)

function GM:Initialize()
	printn("GLMS initializing...")
	
	--Friendly fire should be on
	RunConsoleCommand("mp_friendlyfire", "1")
	
	GAMEMODE.FirstRound = true --Mark as first round so there's an extended ROUND_GRACE
	
	InitiateWaiting()
end

function SendRoundState(state, ply)
	net.Start("RoundState")
		net.WriteInt(state, 16)
	if ply then 
		if IsValid(ply) then
			net.Send(ply)
		end
	else 
		net.Broadcast() 
	end
end

function SetRoundState(state)
	GAMEMODE.RoundState = state
	SendRoundState(state, nil)
end

function GetRoundState()
	return GAMEMODE.RoundState
end

function GetPlayerCount()
	local amount = 0
	for k,v in pairs(player.GetAll()) do
		if IsValid(v) then
			amount = amount + 1
		end
	end
	return amount
end

function EnoughPlayers()
	return GetPlayerCount() >= GetConVar("glms_minimum_players"):GetInt()
end

function GetAlivePlayers()
	local alive = {}
	for k, p in pairs(player.GetAll()) do
		if IsValid(p) and p:Alive() and p:IsSurvivalist() then
			table.insert(alive, p)
		end
	end
	return alive
end

function StopTimers()
	timer.Stop("GLMS_CanStartRoundCheck")
	timer.Stop("GLMS_StartRound")
	timer.Stop("GLMS_EndRound")
end

function DoPlayerCheck()
	if GetConVar("glms_debug"):GetInt() == 1 then return false end
	if not EnoughPlayers() then
		StopTimers()
		InitiateWaiting()
		return false
	end
	return true
end

function CanStartRound()
	if (EnoughPlayers()) or (GetConVar("glms_debug"):GetBool() and GetPlayerCount() >= 1) then
		return true
	end
	return false
end

function StartRoundCheck()
	if CanStartRound() then
		StartRoundGrace()
	end
end

function InitiateWaiting()
	timer.Create("GLMS_CanStartRoundCheck", 5, 0, StartRoundCheck)
	SetRoundState(ROUND_WAITING)
	hook.Call("GLMSRoundStateWaiting")
end

function CleanMap()
	game.CleanUpMap(false, {"env_beam"})
	
	for k,v in pairs(player.GetAll()) do
		if IsValid(v) then
			v:StripWeapons()
		end
	end
end

function StartRoundGrace()
	if GAMEMODE.FirstRound then
		local gracetime = GetConVar("glms_initialgracetime"):GetInt()
		GAMEMODE.FirstRound = false
	else
		local gracetime = GetConVar("glms_gracetime"):GetInt()
	end
	
	timer.Create("GLMS_StartRound", gracetime, 1, StartRound)
	
	CleanMap()
	SpawnPlayers()
	
	SetRoundState(ROUND_GRACE)
	hook.Call("GLMSRoundStateGrace")
end

function SpawnPlayers()
	for k, ply in pairs(plys) do
		if IsValid(ply) then
			ply:DoSpawn()
		end
	end
end

function StartRound()
	if not DoPlayerCheck() then return end
	
	local roundtime = CurTime() + (GetConVar("glms_roundtime"):GetInt() * 60)
	timer.Create("GLMS_EndRound", roundtime, 1, EndRound)
	
	SetRoundState(ROUND_START)
	hook.Call("GLMSRoundStateStart")
end

